package demo.tencent;

import java.util.Set;
import java.util.TreeMap;

public class TencentSign {

    public static final String GET  = "GET";
    public static final String POST = "POST";

    public static String genFinalSignStr(String secretKey, String httpMethod, String httpPath, TreeMap<String,String> params){
        String invokeSignStr = genInvokeSign(httpMethod,httpPath,params);

        return null;
    }

    /**
     *
     * // 请求方法 + 请求主机 +请求路径 + ? + 请求字符串
     *
     * @param httpMethod
     * @param httpPath
     * @param params
     * @return
     */
    public static String genInvokeSign(String httpMethod, String httpPath, TreeMap<String,String> params){
        StringBuilder sbb = new StringBuilder();
        sbb.append(httpMethod);
        sbb.append(httpPath);
        sbb.append("?");
        sbb.append(connectParams(params));

        return sbb.toString();
    }

    /**
     * 拼接参数
     *
     * @param params
     * @return
     */
    public static String connectParams(TreeMap<String,String> params){
        StringBuilder sbb = new StringBuilder();
        Set<String> keySet = params.keySet();

        int i = 0;
        for(String key : keySet){
            sbb.append(String.format("%s=%s", key, params.get(key)));
            i++;
            if(i!=keySet.size()){
                sbb.append("&");
            }
        }
        return sbb.toString();
    }

}
